﻿using System;
using System.Windows;
using System.Windows.Controls;
using DevExpress.Xpf.Core;
using QuanLyKho.Report;
using DevExpress.Xpf.Printing;
using DevExpress.Xpf.Docking;
using QuanLyKho.Server.BLL;
using QuanLyKho.Server;
using DevExpress.XtraRichEdit.API.Word;
using QuanLyKho.Server.Dto;
using System.Collections.Generic;
using System.Linq;


namespace QuanLyKho
{
    /// <summary>
    /// Interaction logic for frmThongKeTongBNChayThan.xaml
    /// </summary>
    public partial class ucThongKeTheKho : UserControl
    {
        #region Fields

        TonKhoThuocBLL tonKhoThuocBLL;
        List<TheKhoDto> listTheKho;
        Dm_Thuoc dmThuoc;

        #endregion

        public ucThongKeTheKho()
        {
            InitializeComponent();
            tonKhoThuocBLL = new TonKhoThuocBLL();

            Dm_ThuocBLL dmThuocBll = new Dm_ThuocBLL();
            cbThuoc.ItemsSource = dmThuocBll.RetrieveAll().OrderBy(u=>u.TenThuocHamLuong);
        }

        private void btnDong_Click(object sender, RoutedEventArgs e)
        {
            DockLayoutManager dockLayoutManager = this.Parent as DockLayoutManager;
            BaseLayoutItem bli = dockLayoutManager.ActiveMDIItem;
            if (bli != null)
            {
                dockLayoutManager.DockController.Close(bli);
            }
        }

        private void btnThongKe_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dmThuoc = cbThuoc.EditValue as Dm_Thuoc;
                if (dmThuoc == null)
                {
                    DXMessageBox.Show("Vui lòng chọn tên thuốc !", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                listTheKho = tonKhoThuocBLL.ThongKeTheKho(dmThuoc.MaThuoc, calendar.StartDate, calendar.EndDate);
                gridThongKe.ItemsSource = listTheKho;
                gridThongKe.RefreshData();

                btnInPhieu.IsEnabled = true;
            }
            catch (Exception)
            {
                DXMessageBox.Show("Thống kê không thành công", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnInPhieu_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                rptTheKho report = new rptTheKho();
                report.BindHeader(dmThuoc);
                report.DataSource = listTheKho;
                report.BindDetails();
                report.ShowPreviewDialog();
            }
            catch (Exception ex)
            {
                DXMessageBox.Show("In phiếu không thành công", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Error);
                DXMessageBox.Show(ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
